package cn.itsource.base.service.impl;

import cn.itsource.base.mapper.BaseMapper;
import cn.itsource.base.query.BaseQuery;
import cn.itsource.base.service.IBaseService;
import cn.itsource.base.util.PageList;
import cn.itsource.org.domain.Department;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    public void add(T t) {
        baseMapper.save(t);
    }

    @Override
    public void remove(Long id) {
        baseMapper.remove(id);
    }

    @Override
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    public T findOne(Long id) {
        return (T) baseMapper.findOne(id);

    }

    @Override
    public List<T> findAll() {
        return baseMapper.findAll();
    }

    @Override
    public PageList<T> queryForPage(BaseQuery tQuery) {
        //1, 查询总条数
        Long count = baseMapper.totalCount(tQuery);
        if(count<=0){ //PageList.data   null
            return new PageList<T>();
        }
        //2, 查询分页数据
        List<T> list = baseMapper.findByQuery(tQuery);
        //3, 封装分页对象PageList
        PageList<T> pageList = new PageList<>(count, list);
        return pageList;
    }
}
